<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
          "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>QUnit test for encrypted PKCS8 private key loading of 'keyutil.js'</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>

<!-- script type="text/javascript" src="qunit.js" /script -->
<!-- link rel="stylesheet" href="qunit.css" type="text/css" media="screen" -->
<script src="http://code.jquery.com/qunit/qunit-1.11.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.11.0.css" type="text/css" media="screen"/>

<script language="JavaScript" type="text/javascript" src="../ext/jsbn.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/jsbn2.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/prng4.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/rng.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/base64.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/rsa.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/rsa2.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/sha1.js"></script>
<script language="JavaScript" type="text/javascript" src="../asn1hex-1.1.js"></script>
<script language="JavaScript" type="text/javascript" src="../rsapem-1.1.js"></script>
<script language="JavaScript" type="text/javascript" src="../rsasign-1.2.js"></script>
<script language="JavaScript" type="text/javascript" src="../crypto-1.1.js"></script>

<script language="JavaScript" type="text/javascript" src="../ext/prng4.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/rng.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/ec.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/ec-patch.js"></script>
<script language="JavaScript" type="text/javascript" src="../ecdsa-modified-1.0.js"></script>
<script language="JavaScript" type="text/javascript" src="../ecparam-1.0.js"></script>

<script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script>
<script language="JavaScript" type="text/javascript" src="../asn1-1.0.js"></script>

<!-- jsrsasign keyutil external codes -->
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/x64-core.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/cipher-core.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/tripledes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/md5.js"></script>
<!-- for PKCS5 PBKDF2 -->
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha1.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/hmac.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/pbkdf2.js"></script>

<script src="../keyutil-1.0.js"></script>

<script type="text/javascript">
<!--
$(document).ready(function(){

// _gitpg/jsrsasign/test/eckey/k1.prv.p8e.pem pass:passwd
var k1PrvP8ePem = "" +
"-----BEGIN ENCRYPTED PRIVATE KEY-----\n" +
"MIHVMEAGCSqGSIb3DQEFDTAzMBsGCSqGSIb3DQEFDDAOBAj20JdDrZ2aywICCAAw\n" +
"FAYIKoZIhvcNAwcECMUYjGnyWlEZBIGQlK5TFonZXOFuu+UGi9VvH399ps4QHRe5\n" +
"ddRaWYUvjnKoA1kR8NorNzpAxwFeC0tGC9FHs8EV8gE1kCRJS0TBfn9Z3N/I+FEo\n" +
"s6qwahGeiHVJZQc6sHg8ch/At80D+2EpuQDhL/DYPYzDcxema7QE3gi0Q6rEuxgv\n" +
"1yqXRRTZGKyiNjmozXY/F0G6OQsT8BlA\n" +
"-----END ENCRYPTED PRIVATE KEY-----\n";

// _gitpg/jsrsasign/test/eckey/k1.prv.p8p.hex
var k1PrvP8pHex = "308187020100301306072a8648ce3d020106082a8648ce3d030107046d306b020101042011b5737cf9d93f17c0cb1a84655d3995a02824097effa5edd8ee26381eb5d6c3a14403420004a01532a3c0900053de60fbefefcca58793301598d308b41e6f4e364e388c2711bef432c599148c94143d4ff46c2cb73e3e6a41d7eef23c047ea11e60667de425";

// _test/z1.prv.p8e.pem
var z1PrvP8ePem = "" +
"-----BEGIN ENCRYPTED PRIVATE KEY-----\n" +
"MIIBpjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIyScsGkpMl3kCAggA\n" +
"MBQGCCqGSIb3DQMHBAiObBK7oyBAzwSCAWCkStExnU3pYBQAxRqDctg5QEO3Ic1d\n" +
"GsRW+JW0kNonUydoOSD/7DBglnSDowiQ69HCW4OmumSJIU0hewX0yburFuy6Zs/q\n" +
"DkWox7oxBCsgFmfmkKGcK0USeJ+LX+YZVbvCCvHZkd99pEqQSfkRu4DgXbWmo6Zv\n" +
"2A6VFyBhE9MFmYex8mHTZ5cchBzAiFWRzjpRX94F4vbUFoXPGrEbOLovM2VpZ8Df\n" +
"1nLuXhWYVHsXHWn+fm7TH4Xzh1mFYYkTiXL9ABwKKohEL2SQJzKpa9FZvOZAmlSe\n" +
"UgQxVQLM/ZC+o4Vs4JHqIu4Ek2bgjEqDzo2EPznpcCsQTcwOqhPwNjP4Y3ovn1Nm\n" +
"8i0Hx51OA0vjRZBvvduKM9d5kRAOg8jqLjeWU3KraWLuSJ96RTzlWFgw6PsML+RE\n" +
"LHfdKDjRT0NMEMRh892oBi828asvvSWO6HYcM0xclt0uakxJhVP/mtzA\n" +
"-----END ENCRYPTED PRIVATE KEY-----\n";

// _test/z1.prv.p8p.hex
var z1PrvP8pHex = "30820154020100300d06092a864886f70d01010105000482013e3082013a020100024100e8664dd2b40529121568f3b39bc97a62e7ba3c09babdc4f0dcd8df90eb790b9bb645a2b70e3112747b4d3c41b51424895115fef88f79d43eae5b1a4e3518fd590203010001024078bbc54c646795e9a623cb0f912c7f9f6861711560e57bade21ded2f7d9579f86272fab9e71ab27282c2760c404dae6ac8a3ab852cf6bcd562f9aff8247a264d022100fe9bd450866b3255614fde976a76ac0b1d5830dda64f5359b946d8512bc1239f022100e9ab680f5f05dbb0f4311b0e6f4368f4874a1238979d8bc600c35c6f20707c07022100d549cb698dc17d4fe30e5a84f3ade860033c1eeb7f67d286465c9fd817d45b3702201849b4bb44493b989d092da2c675df46eb790b83ce5e95d4b2e79b88017b2fa902207d261a96256d49b020cb1d587ecde127e093a4d2b34cdbff171f34125b5857a9";

test("getKeyFromEncryptedPKCS8PEM z1(RSA)", function() {
  var key = KEYUTIL.getKeyFromEncryptedPKCS8PEM(z1PrvP8ePem, "passwd");
  expect(9);
  equal(key.type, "RSA", "type");
  equal(key.n.compareTo(new BigInteger("00e8664dd2b40529121568f3b39bc97a62e7ba3c09babdc4f0dcd8df90eb790b9bb645a2b70e3112747b4d3c41b51424895115fef88f79d43eae5b1a4e3518fd59", 16)), 0, "n");
  equal(key.e, 65537, "e(65537)");
  equal(key.d.compareTo(new BigInteger("78bbc54c646795e9a623cb0f912c7f9f6861711560e57bade21ded2f7d9579f86272fab9e71ab27282c2760c404dae6ac8a3ab852cf6bcd562f9aff8247a264d", 16)), 0, "d");
  equal(key.p.compareTo(new BigInteger("00fe9bd450866b3255614fde976a76ac0b1d5830dda64f5359b946d8512bc1239f", 16)), 0, "p");
  equal(key.q.compareTo(new BigInteger("00e9ab680f5f05dbb0f4311b0e6f4368f4874a1238979d8bc600c35c6f20707c07", 16)), 0, "q");
  equal(key.dmp1.compareTo(new BigInteger("00d549cb698dc17d4fe30e5a84f3ade860033c1eeb7f67d286465c9fd817d45b37", 16)), 0, "dp");
  equal(key.dmq1.compareTo(new BigInteger("1849b4bb44493b989d092da2c675df46eb790b83ce5e95d4b2e79b88017b2fa9", 16)), 0, "dq");
  equal(key.coeff.compareTo(new BigInteger("7d261a96256d49b020cb1d587ecde127e093a4d2b34cdbff171f34125b5857a9", 16)), 0, "co");
});

test("getKeyFromEncryptedPKCS8PEM k1(ECC)", function() {
  var key = KEYUTIL.getKeyFromEncryptedPKCS8PEM(k1PrvP8ePem, "passwd");
  expect(3);
  equal(key.type, "EC", "type");
  equal(key.curveName, "secp256r1", "curveName");
  equal(key.prvKeyHex, "11b5737cf9d93f17c0cb1a84655d3995a02824097effa5edd8ee26381eb5d6c3", "prvKeyHex");
});

test("parsePlainPrivatePKCS8Hex z1(RSA)", function() {
  var r = KEYUTIL.parsePlainPrivatePKCS8Hex(z1PrvP8pHex);
  expect(3);
  equal(r.algoid, "2a864886f70d010101", "algoid");
  equal(r.algparam, null, "algparam");
  equal(r.keyidx, 52, "keyidx");
});

test("parsePlainPrivatePKCS8Hex k1(EC)", function() {
  var r = KEYUTIL.parsePlainPrivatePKCS8Hex(k1PrvP8pHex);
  expect(3);
  equal(r.algoid, "2a8648ce3d0201", "algoid");
  equal(r.algparam, "2a8648ce3d030107", "algparam");
  equal(r.keyidx, 58, "keyidx");
});

test("parsePrivateRawECKeyHexAtObj k1(EC)", function() {
  var r = KEYUTIL.parsePlainPrivatePKCS8Hex(k1PrvP8pHex);
  KEYUTIL.parsePrivateRawECKeyHexAtObj(k1PrvP8pHex, r);
  expect(1);
  equal(r.key, "11b5737cf9d93f17c0cb1a84655d3995a02824097effa5edd8ee26381eb5d6c3", "key");
});

test("parsePrivateRawRSAKeyHexAtObj z1(RSA)", function() {
  var r = KEYUTIL.parsePlainPrivatePKCS8Hex(z1PrvP8pHex);
  KEYUTIL.parsePrivateRawRSAKeyHexAtObj(z1PrvP8pHex, r);
  expect(8);
  equal(r.key.n, "00e8664dd2b40529121568f3b39bc97a62e7ba3c09babdc4f0dcd8df90eb790b9bb645a2b70e3112747b4d3c41b51424895115fef88f79d43eae5b1a4e3518fd59", "n");
  equal(r.key.e, "010001", "e");
  equal(r.key.d, "78bbc54c646795e9a623cb0f912c7f9f6861711560e57bade21ded2f7d9579f86272fab9e71ab27282c2760c404dae6ac8a3ab852cf6bcd562f9aff8247a264d", "d");
  equal(r.key.p, "00fe9bd450866b3255614fde976a76ac0b1d5830dda64f5359b946d8512bc1239f", "p");
  equal(r.key.q, "00e9ab680f5f05dbb0f4311b0e6f4368f4874a1238979d8bc600c35c6f20707c07", "q");
  equal(r.key.dp, "00d549cb698dc17d4fe30e5a84f3ade860033c1eeb7f67d286465c9fd817d45b37", "dp");
  equal(r.key.dq, "1849b4bb44493b989d092da2c675df46eb790b83ce5e95d4b2e79b88017b2fa9", "dq");
  equal(r.key.co, "7d261a96256d49b020cb1d587ecde127e093a4d2b34cdbff171f34125b5857a9", "co");
});

test("RSAKey/KJUR.crypto.ECDSA instanceof operator test", function() {
  var k1 = new RSAKey();
  var k2 = new KJUR.crypto.ECDSA();
  expect(4);
  equal(k1 instanceof RSAKey, true, "k1 instanceof RSAKey");
  equal(k2 instanceof RSAKey, false, "k2 instanceof RSAKey");
  equal(k1 instanceof KJUR.crypto.ECDSA, false, "k1 instanceof KJUR.crypto.ECDSA");
  equal(k2 instanceof KJUR.crypto.ECDSA, true, "k2 instanceof KJUR.crypto.ECDSA");
});

});
-->
</script>
  
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">test markup</div>
QUnit for 
<a href="qunit-do-keyutil.html">keyutil</a> | 
<a href="qunit-do-keyutil-eprv.html">keyutil-eprv</a> | 
<a href="qunit-do-keyutil-pub.html">keyutil-pub</a> | 
</body>
</html>

